package tbl_robot

import (
	"fmt"
	"gitee.com/huerbiao/robot/pkg/mysql"
	"reflect"
)

func AddTblRobot(tbl TblRobot) (int64, error) {
	oneDt := make(map[string]interface{})
	// 反射获取结构体的所有字段
	t := reflect.TypeOf(tbl)
	v := reflect.ValueOf(tbl)

	//[]string{"id", "ctime", "utime"}
	continueField := map[string]struct{}{
		"id":    {},
		"ctime": {},
		"utime": {},
	}
	// 遍历结构体的所有字段
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		fieldName := field.Tag.Get("json")
		if _, ok := continueField[fieldName]; ok {
			continue
		}
		fieldContent := v.Field(i).Interface()
		if fieldContent == "" {
			continue
		}
		// 使用json标签作为map的键
		oneDt[fieldName] = v.Field(i).Interface()
	}
	fmt.Printf("oneDt: %v\n", oneDt)
	return Insert(mysql.RobotWrite, []map[string]interface{}{oneDt})
}
